package content;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.commons.io.IOUtils;

import com.ctc.wstx.util.URLUtil;

public class FileSystemUploader implements Uploader {
	private static String	directory	= "D:/Dropbox/cmss/";
	
	public FileSystemUploader() {
		File file = new File(directory);
		if (file.exists() == false) {
			file.mkdir();
		}
	}
	
	public String upload(InputStream is, String fileName) {
		try {
			byte[] content = IOUtils.toByteArray(is);
			return upload(content, fileName);
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String upload(byte[] content, String fileName) {
		String filePath = directory + fileName;
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(filePath);
			fos.write(content);
			return URLUtil.uriFromSystemId(filePath).toString();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) throws IOException {
		InputStream is = URI.create("https://www.facebook.com/").toURL().openStream();
		Uploader up = new FileSystemUploader();
		System.out.println(up.upload(is, "index.php"));
	}
}
